#!/bin/bash

NDK=/path/to/android-ndk-r28c
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
SYSROOT=$TOOLCHAIN/sysroot
API=21
SPEEX_ROOT=/path/to/speex

for TARGET in \
    "armv7a-linux-androideabi aarch-armeabi-v7a 21" \
    "aarch64-linux-android aarch64 21" \
    "i686-linux-android i686 21" \
    "x86_64-linux-android x86_64 21"
do
    set -- $TARGET
    TARGET_ABI=$1
    ANDROID_ABI=$2
    API=$3

    CC=$TOOLCHAIN/bin/$TARGET_ABI$API-clang
    CXX=$TOOLCHAIN/bin/$TARGET_ABI$API-clang++
    AR=$TOOLCHAIN/bin/llvm-ar
    RANLIB=$TOOLCHAIN/bin/llvm-ranlib
    STRIP=$TOOLCHAIN/bin/llvm-strip

    PREFIX=/output/speex-android/$ANDROID_ABI

    echo "Building for $ANDROID_ABI..."

    mkdir -p build-$ANDROID_ABI && cd build-$ANDROID_ABI

    $SPEEX_ROOT/configure \
      --host=$TARGET_ABI \
      --prefix=$PREFIX \
      --enable-static \
      --disable-shared \
      --disable-binaries \
      --disable-oggtest \
      --disable-celt \
      --without-ogg \
      --with-sysroot=$SYSROOT \
      CC="$CC" CXX="$CXX" AR="$AR" RANLIB="$RANLIB" STRIP="$STRIP" \
      CFLAGS="-O2 -fPIC" LDFLAGS=""

    make -j8 && make install
    cd ..
done